package com.ygxsk.carhome.alertview;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blankj.utilcode.util.ColorUtils;
import com.ygxsk.carhome.R;

import java.util.List;

//import com.dgj.dinggovern.R;

/**
 * Created by Sai on 15/8/9.
 */
public class AlertViewAdapter extends BaseAdapter {
    private final List<String> mDatas;
    private final List<String> mDestructive;
    private final String title;

    public AlertViewAdapter(List<String> datas, List<String> destructive, String _title) {

        this.mDatas = datas;
        this.mDestructive = destructive;
        this.title = _title;

    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        String data = mDatas.get(position);

        Holder holder = null;

        View view = convertView;

        if (view == null) {

            LayoutInflater inflater = LayoutInflater.from(parent.getContext());

            view = inflater.inflate(R.layout.item_alertbutton, null);

            View cutupLine = (View) view.findViewById(R.id.itemcutupline);

            //如果没有标题,那么第一个横线要去掉
            if (!TextUtils.isEmpty(title)) {//如果标题不是空，那么就是所有的横线都展示出来

                cutupLine.setVisibility(View.VISIBLE);

            } else {
                //如果标题是空的，那么第一条分割细线该是隐藏掉的
                if (position == 0) {
                    cutupLine.setVisibility(View.GONE);
                } else {
                    cutupLine.setVisibility(View.VISIBLE);
                }
            }

            holder = creatHolder(view);

            view.setTag(holder);

        } else {
            holder = (Holder) view.getTag();

        }


        holder.UpdateUI(parent.getContext(), data, position);

        return view;
    }

    public Holder creatHolder(View view) {
        return new Holder(view);
    }

    class Holder {

        private final TextView tvAlert;

        public Holder(View view) {
            tvAlert = (TextView) view.findViewById(R.id.tvAlert);
        }

        public void UpdateUI(Context context, String data, int position) {

            tvAlert.setText(data);

            if (mDestructive != null && mDestructive.contains(data)) {
                //tvAlert.setTextColor(context.getResources().getColor(R.color.textColor_alert_button_destructive));
                tvAlert.setTextColor(ColorUtils.getColor(R.color.textColor_alert_button_destructive));
            } else {
                //2018年04月05日itchenqi自己修改的黑色字体
                //tvAlert.setTextColor(context.getResources().getColor(R.color.black));
                //原始代码
                //tvAlert.setTextColor(context.getResources().getColor(R.color.textColor_alert_button_others));
                tvAlert.setTextColor(ColorUtils.getColor(R.color.textColor_alert_button_others));
            }
        }
    }
}